﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using com.zhongjh.Library.BaseClass;
using com.zhongjh.Library.Configuration;

namespace ConfiguratorForWinForm
{
    public partial class MainForm : FormBase
    {
        #region 字段

        MainFormView mMainFormView = null;

        #endregion //end 字段

        #region 方法

        public MainForm()
        {
            InitializeComponent();
        }

        public override void InitBase()
        {
            mMainFormView = new MainFormView(this);
            base.InitSetView(mMainFormView);
        }

        #endregion //end 方法

        #region 事件

        //窗口加载事件
        private void MainForm_Load(object sender, EventArgs e)
        {
            mMainFormView.MainFormLoad();
        }

        //树节点选择事件
        private void m_tvConfig_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                mMainFormView.SelectNode(e.Node);
            }
        }

        //树菜单点击事件
        private void m_configMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Name == "m_miAddNode") //添加子节点
            {
                mMainFormView.AddNewNode();
            }
            else if (e.ClickedItem.Name == "m_miDeleteNode") //删除节点
            {
                mMainFormView.DeleteNode();
            }
        }
        
        //键改变事件
        private void m_txtKey_TextChanged(object sender, EventArgs e)
        {
            TreeNode selectNode = m_gbConfigNode.Tag as TreeNode;
            if (selectNode != null)
            {
                ConfigItem selectItem = selectNode.Tag as ConfigItem;
                if (selectItem != null)
                {
                    selectItem.Key = m_txtKey.Text;
                    selectNode.Text = string.Format("[{0}].[{1}]", selectItem.Key, selectItem.Value);
                }
            }
        }

        //值改变事件
        private void m_txtValue_TextChanged(object sender, EventArgs e)
        {
            TreeNode selectNode = m_gbConfigNode.Tag as TreeNode;
            if (selectNode != null)
            {
                ConfigItem selectItem = selectNode.Tag as ConfigItem;
                if (selectItem != null)
                {
                    selectItem.Value = m_txtValue.Text;
                    selectNode.Text = string.Format("[{0}].[{1}]", selectItem.Key, selectItem.Value);
                }
            }
        }

        //备注改变事件
        private void m_txtRemark_TextChanged(object sender, EventArgs e)
        {
            TreeNode selectNode = m_gbConfigNode.Tag as TreeNode;
            if (selectNode != null)
            {
                ConfigItem selectItem = selectNode.Tag as ConfigItem;
                if (selectItem != null)
                {
                    selectItem.Remark = m_txtRemark.Text;
                }
            }
        }

        //菜单点击事件
        private void m_mainMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Name == "m_menuLoadConfig")
            {
                mMainFormView.LoadConfig();
            }
            else if (e.ClickedItem.Name == "m_menuSaveConfig")
            {
                mMainFormView.SaveConfig();
            }
            else if (e.ClickedItem.Name == "m_menuHelp")
            {
            }
        }

        #endregion //end 事件


    }
}
